package com.mini.cn.goods.io.netty.io;

import io.netty.bootstrap.ServerBootstrapConfig;
import io.netty.channel.ChannelOption;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.util.internal.PlatformDependent;

/**
 * Created with IntelliJ IDEA.
 * User: fly
 * Date: 2020-03-28
 * Time: 10:11 上午
 */
public final class NettyConfig implements IFourLayerConfig {

    final ServerBootstrapConfig config;

    public NettyConfig(ServerBootstrapConfig config) {
        this.config = config;
    }

    @Override
    public long getConnectTimeout() {
        return (long) config.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS);
    }

    @Override
    public int getBackLog() {
        return (int) config.options().get(ChannelOption.SO_BACKLOG);
    }

    @Override
    public boolean ifdirectBuffer() {
        return PlatformDependent.directBufferPreferred();
    }

    @Override
    public int getWriteLoopCount() {
        return (int) config.childOptions().get(ChannelOption.WRITE_SPIN_COUNT);
    }

    @Override
    public String getBufferWaterMark() {
        WriteBufferWaterMark writeBufferWaterMark = (WriteBufferWaterMark) config.childOptions().
                get(ChannelOption.WRITE_BUFFER_WATER_MARK);
        return writeBufferWaterMark.high() + ":" + writeBufferWaterMark.low();
    }

    @Override
    public boolean isNoNagle() {
        return (boolean) config.childOptions().get(ChannelOption.TCP_NODELAY);
    }

    @Override
    public boolean isKeepLive() {
        return (boolean) config.childOptions().get(ChannelOption.SO_KEEPALIVE);
    }

    @Override
    public int getLinger() {
        return (int) config.childOptions().get(ChannelOption.SO_LINGER);
    }

    @Override
    public boolean ifReuseAddr() {
        return (boolean) config.childOptions().get(ChannelOption.ALLOW_HALF_CLOSURE);
    }

    @Override
    public boolean ifAllowHalfClosure() {
        return (boolean) config.childOptions().get(ChannelOption.SO_REUSEADDR);
    }

}
